#include "stdafx.h"
#include "PackOut.h"

CPackOut::CPackOut()
{
}

CPackOut::~CPackOut()
{
}

bool CPackOut::ResetBuffer()
{
	m_strBuffer.Empty();
	return true;
}

bool CPackOut::Append(char* pBuffer, int nLen)
{
	m_strBuffer.Append(pBuffer, nLen);
	return true;
}

bool CPackOut::GetByte(byte& cByte)
{
	if (m_strBuffer.GetLength() < sizeof(byte))
		return false;
	memcpy_s(&cByte, sizeof(byte), m_strBuffer.GetBuffer(), sizeof(byte));
	m_strBuffer = m_strBuffer.Right(m_strBuffer.GetLength() - sizeof(byte));
	return true;
}

bool CPackOut::GetWord(WORD& wWord)
{
	if (m_strBuffer.GetLength() < sizeof(WORD))
		return false;
	memcpy_s(&wWord, sizeof(WORD), m_strBuffer.GetBuffer(), sizeof(WORD));
	wWord = ntohs(wWord);
	m_strBuffer = m_strBuffer.Right(m_strBuffer.GetLength() - sizeof(WORD));
	return true;
}

bool CPackOut::GetInt(int& nInt)
{
	if (m_strBuffer.GetLength() < sizeof(int))
		return false;
	memcpy_s(&nInt, sizeof(int), m_strBuffer.GetBuffer(), sizeof(int));
	nInt = ntohl(nInt);
	m_strBuffer = m_strBuffer.Right(m_strBuffer.GetLength() - sizeof(int));
	return true;
}

bool CPackOut::GetDWord(DWORD& dwDWrod)
{
	if (m_strBuffer.GetLength() < sizeof(DWORD))
		return false;
	memcpy_s(&dwDWrod, sizeof(DWORD), m_strBuffer.GetBuffer(), sizeof(DWORD));
	dwDWrod = ntohl(dwDWrod);
	m_strBuffer = m_strBuffer.Right(m_strBuffer.GetLength() - sizeof(DWORD));
	return true;
}

bool CPackOut::GetString(CString& strString)
{
	int nLen = 0;
	if (GetInt(nLen) == false) return false;
	if (m_strBuffer.GetLength() < nLen)
		return false;
	strString.Empty();
	strString.Append((TCHAR*)m_strBuffer.GetBuffer(), nLen);
	m_strBuffer = m_strBuffer.Right(m_strBuffer.GetLength() - nLen);
	return true;
}

bool CPackOut::GetString(CStringA& strString)
{
	int nLen = 0;
	if (GetInt(nLen) == false) return false;
	if (m_strBuffer.GetLength() < nLen)
		return false;
	strString.Append((char*)m_strBuffer.GetBuffer(), nLen);
	m_strBuffer = m_strBuffer.Right(m_strBuffer.GetLength() - nLen);
	return true;
}

char* CPackOut::GetBuffer()
{
	return m_strBuffer.GetBuffer();
}

int CPackOut::GetLength()
{
	return m_strBuffer.GetLength();
}


